package com.gzhryc.shared_device.oem.code.order;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.json.JsonTools;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.common.redis.RedisTools;
import com.gzhryc.shared_device.bxscn.code.dao.db.BXSCNOrder;
import com.gzhryc.shared_device.cdb.code.dao.db.CDBOrder;
import com.gzhryc.shared_device.cdcz.code.dao.db.CDCZOrder;
import com.gzhryc.shared_device.oem.code.RedisDBConstants;
import com.gzhryc.shared_device.pcl.code.dao.db.PCLOrder;
import org.apache.commons.lang3.StringUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.ScanParams;
import redis.clients.jedis.ScanResult;

import java.util.ArrayList;
import java.util.List;

public class RedisCacheManager {

    static Logger log = Logger.getLogger(RedisCacheManager.class);

    public static List<CacheOrderCollect> findCacheOrderCollect() {
        Jedis jedis = RedisTools.get().getJedis();
        if (jedis != null) {
            try {
                List<CacheOrderCollect> orderList = new ArrayList<>();
                jedis.select(RedisDBConstants.ORDER_CACHE_DB);
                String cursor = ScanParams.SCAN_POINTER_START;
                do {
                    ScanResult<String> result = jedis.scan(cursor);
                    if (result == null)
                        break;
                    cursor = result.getCursor();
                    if (result != null && result.getResult() != null) {
                        for (String key : result.getResult()) {
                            String obj = jedis.get(key);
                            if (StringUtils.isNoneBlank(obj)) {
                                orderList.add(JsonTools.fromJson(obj, CacheOrderCollect.class));
                            }
                        }
                    }
                } while (!cursor.equals(ScanParams.SCAN_POINTER_START));
                return orderList;
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            } finally {
                jedis.close();
            }
        }
        return null;
    }

    public static CacheOrderCollect getCacheOrderCollect(String tradeNo){
        String join = RedisTools.get().get(RedisDBConstants.ORDER_CACHE_DB,tradeNo);
        if(StringTools.isNotBlank(join)){
            return JsonTools.fromJson(join, CacheOrderCollect.class);
        }
        return null;
    }


    public static void removeCacheOrderCollect(String tradeNo){
        RedisTools.get().delete(RedisDBConstants.ORDER_CACHE_DB,tradeNo);
    }

    //=====充电插座=====

    public static List<CDCZOrder> findWorkingCDCZDeviceOrder() {
        Jedis jedis = RedisTools.get().getJedis();
        if (jedis != null) {
            try {
                List<CDCZOrder> orderList = new ArrayList<>();
                jedis.select(RedisDBConstants.CDCZ_ORDER_WORKING_DB);
                String cursor = ScanParams.SCAN_POINTER_START;
                do {
                    ScanResult<String> result = jedis.scan(cursor);
                    if (result == null)
                        break;
                    cursor = result.getCursor();
                    if (result != null && result.getResult() != null) {
                        for (String key : result.getResult()) {
                            String obj = jedis.get(key);
                            if (StringUtils.isNoneBlank(obj)) {
                                orderList.add(JsonTools.fromJson(obj, CDCZOrder.class));
                            }
                        }
                    }
                } while (!cursor.equals(ScanParams.SCAN_POINTER_START));
                return orderList;
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            } finally {
                jedis.close();
            }
        }
        return null;
    }

    public static CDCZOrder getWorkingCDCZDeviceOrder(String tradeNo){
        String join = RedisTools.get().get(RedisDBConstants.CDCZ_ORDER_WORKING_DB,tradeNo);
        if(StringTools.isNotBlank(join)){
            return JsonTools.fromJson(join, CDCZOrder.class);
        }
        return null;
    }

    public static boolean saveWorkingCDCZDeviceOrder(CDCZOrder deviceOrder){
        return RedisTools.get().set(RedisDBConstants.CDCZ_ORDER_WORKING_DB,deviceOrder.getTradeNo(),JsonTools.toJson(deviceOrder));
    }

    public static void removeWorkingCDCZDeviceOrder(String tradeNo){
        RedisTools.get().delete(RedisDBConstants.CDCZ_ORDER_WORKING_DB,tradeNo);
    }

    //=====共享充电宝=====

    public static List<CDBOrder> findWorkingCDBDeviceOrder() {
        Jedis jedis = RedisTools.get().getJedis();
        if (jedis != null) {
            try {
                List<CDBOrder> orderList = new ArrayList<>();
                jedis.select(RedisDBConstants.CDB_ORDER_WORKING_DB);
                String cursor = ScanParams.SCAN_POINTER_START;
                do {
                    ScanResult<String> result = jedis.scan(cursor);
                    if (result == null)
                        break;
                    cursor = result.getCursor();
                    if (result != null && result.getResult() != null) {
                        for (String key : result.getResult()) {
                            String obj = jedis.get(key);
                            if (StringUtils.isNoneBlank(obj)) {
                                orderList.add(JsonTools.fromJson(obj, CDBOrder.class));
                            }
                        }
                    }
                } while (!cursor.equals(ScanParams.SCAN_POINTER_START));
                return orderList;
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            } finally {
                jedis.close();
            }
        }
        return null;
    }

    public static CDBOrder getWorkingCDBDeviceOrder(String tradeNo){
        String join = RedisTools.get().get(RedisDBConstants.CDB_ORDER_WORKING_DB,tradeNo);
        if(StringTools.isNotBlank(join)){
            return JsonTools.fromJson(join, CDBOrder.class);
        }
        return null;
    }

    public static boolean saveWorkingCDBDeviceOrder(CDBOrder deviceOrder){
        return RedisTools.get().set(RedisDBConstants.CDB_ORDER_WORKING_DB,deviceOrder.getTradeNo(),JsonTools.toJson(deviceOrder));
    }

    public static void removeWorkingCDBDeviceOrder(String tradeNo){
        RedisTools.get().delete(RedisDBConstants.CDB_ORDER_WORKING_DB,tradeNo);
    }

    //=====便捷线充=====

    public static List<PCLOrder> findWorkingPCLDeviceOrder() {
        Jedis jedis = RedisTools.get().getJedis();
        if (jedis != null) {
            try {
                List<PCLOrder> orderList = new ArrayList<>();
                jedis.select(RedisDBConstants.PCL_ORDER_WORKING_DB);
                String cursor = ScanParams.SCAN_POINTER_START;
                do {
                    ScanResult<String> result = jedis.scan(cursor);
                    if (result == null)
                        break;
                    cursor = result.getCursor();
                    if (result != null && result.getResult() != null) {
                        for (String key : result.getResult()) {
                            String obj = jedis.get(key);
                            if (StringUtils.isNoneBlank(obj)) {
                                orderList.add(JsonTools.fromJson(obj, PCLOrder.class));
                            }
                        }
                    }
                } while (!cursor.equals(ScanParams.SCAN_POINTER_START));
                return orderList;
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            } finally {
                jedis.close();
            }
        }
        return null;
    }

    public static boolean saveWorkingPCLDeviceOrder(PCLOrder deviceOrder){
        return RedisTools.get().set(RedisDBConstants.PCL_ORDER_WORKING_DB,deviceOrder.getTradeNo(),JsonTools.toJson(deviceOrder));
    }

    public static void removeWorkingPCLDeviceOrder(String tradeNo){
        RedisTools.get().delete(RedisDBConstants.PCL_ORDER_WORKING_DB,tradeNo);
    }

    //便携式储能电池

    public static List<BXSCNOrder> findWorkingBXSCNDeviceOrder() {
        Jedis jedis = RedisTools.get().getJedis();
        if (jedis != null) {
            try {
                List<BXSCNOrder> orderList = new ArrayList<>();
                jedis.select(RedisDBConstants.BXSCN_DEVICE_DB);
                String cursor = ScanParams.SCAN_POINTER_START;
                do {
                    ScanResult<String> result = jedis.scan(cursor);
                    if (result == null)
                        break;
                    cursor = result.getCursor();
                    if (result != null && result.getResult() != null) {
                        for (String key : result.getResult()) {
                            String obj = jedis.get(key);
                            if (StringUtils.isNoneBlank(obj)) {
                                orderList.add(JsonTools.fromJson(obj, BXSCNOrder.class));
                            }
                        }
                    }
                } while (!cursor.equals(ScanParams.SCAN_POINTER_START));
                return orderList;
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            } finally {
                jedis.close();
            }
        }
        return null;
    }

    public static BXSCNOrder getWorkingBXSCNDeviceOrder(String tradeNo){
        String join = RedisTools.get().get(RedisDBConstants.BXSCN_ORDER_WORKING_DB,tradeNo);
        if(StringTools.isNotBlank(join)){
            return JsonTools.fromJson(join, BXSCNOrder.class);
        }
        return null;
    }

    public static boolean saveWorkingBXSCNDeviceOrder(BXSCNOrder deviceOrder){
        return RedisTools.get().set(RedisDBConstants.BXSCN_ORDER_WORKING_DB,deviceOrder.getTradeNo(),JsonTools.toJson(deviceOrder));
    }

    public static void removeWorkingBXSCNDeviceOrder(String tradeNo){
        RedisTools.get().delete(RedisDBConstants.BXSCN_ORDER_WORKING_DB,tradeNo);
    }

}
